<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/20
 * Time: 19:42
 */

namespace app\index\controller;


use think\Controller;
use think\Db;
use think\Session;

class Magazine extends Controller
{
    public function index(){
        return view();
    }
    public function insertMagazine(){
        return view();
    }
    public function magazineMe(){
        return view();
    }

    //获取所有的期刊信息
    public function getMagazine(){
        $page = input('page');
        $limit = input('limit');


        $start = ($page-1) * $limit;

        $result['code'] = 1;
        $result['count'] = 0;

        $result['data'] = Db::table('magazine')
            ->order('Id desc')
            ->limit($start,$limit)
            ->select();
        $result['count'] = count(Db::table('magazine')->select());
        if(!empty($result['data'])){
            $result['msg'] = '成功获得数据';
        }
        else{
            $result['code'] = 0;
            $result['msg'] = '无数据';
        }
        return \json($result);
    }

    //只获取当前用户插入的期刊信息
    public function getMineMagazine(){
        $page = input('page');
        $limit = input('limit');
        $user = Session::get('user')->name;


        $start = ($page-1) * $limit;

        $result['code'] = 1;
        $result['count'] = 0;

        $result['data'] = Db::table('magazine')
            ->where('user','=',$user)
            ->limit($start,$limit)
            ->select();
        $result['count'] = count(Db::table('magazine')->select());
        if(!empty($result['data'])){
            $result['msg'] = '成功获得数据';
        }
        else{
            $result['code'] = 0;
            $result['msg'] = '无数据';
        }
        return \json($result);

    }

    //插入期刊信息
    public function addMagazine(){
        $data['user'] = Session::get('user')->name;
        $data['name'] = input('name');
        $data['circle'] = input('circle');
        $data['price'] = input('price');
        $data['remark'] = input('remark');

        $ret = Db::table('magazine')->insert($data);

        if($ret){
            $this->success('插入成功');
        }
        else{
            $this->error('插入失败');
        }

    }

    //删除期刊信息
    public function delMagazine(){
        $id = input('id');
        $ret = Db::table('magazine')->where('Id','=',$id)->delete();
        if($ret){
            $this->success('删除成功');
        }
        else{
            $this->error('删除失败');
        }
    }

}